﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using com.fukefu;
using com.fukefu.accout;
using com.fukefu.info;
using com.fukefu.report;
using com.fukefu.template;
using com.fukefu.update;
using com.fukefu.update.instantiated;
using com.fukefu.url;

namespace 智能群发
{
    public partial class MainForm : BaseForm
    {
        public MainForm()
        {
            InitializeComponent();
            this.MessageMenuItem.Name = "MessageMenuItem";
            this.UpdateTitleMenuItem.Name = "UpdateTitleMenuItem";
            StateBar();
        }
        //退出
        private void Exit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        //关闭按钮
        private void MainForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }
        //更新标题
        private void UpdateTitleMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowCheckDialog("您确定重新生成标题?"))
            {
                UpdateTitleMenuItem.Enabled = false;
                new Thread(new ThreadStart(UpdateTitle)).Start();
            }
        }
        //标题重新组合
        private void UpdateTitle()
        {
            new KeywordToTitle().Update();
            UpdateTitleMenuItem.Enabled = true;
            ShowMessageDialog("标题从新组合结束!");
        }
        //收录检测
        private void shoulu_Click(object sender, EventArgs e)
        {
            ShowForm(UrlShouluForm.GetInstance());
        }
        //报表
        private void report_Click(object sender, EventArgs e)
        {
            ShowForm(ReportForm.GetInstance());
        }
        //新浪blog
        private void SinaBlogMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(UpdateForm.GetInstance(new SinaBlogPost()));
        }
        //信息发布
        private void MessageMenuItem_Click(object sender, EventArgs e)
        {
            MessageMenuItem.Visible = false;
            UpdateTitleMenuItem.Enabled = false;
            ShowForm(PostForm.GetInstance());
        }
        //更新排名
        private void UpdatePaiming_Click(object sender, EventArgs e)
        {
            ShowForm(UpdateForm.GetInstance(new Market()));
        }
        //用户管理
        private void AccountMenu_Click(object sender, EventArgs e)
        {
            ShowForm(AccountForm.GetInstance());
        }
        //发布统计
        private void TongJiMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(new StatisticsForm("发布统计", "select title as 域名, count(1) as 总和 from 发布总表 where (datediff(d, dt, getdate()) = 0) group by title order by 总和 desc"));
        }
        //查看发布文章
        private void PublicMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(PublishReportForm.GetInstance());
        }
        //数据报表
        private void RepeatMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(ReportForm.GetInstance());
        }
        //退出
        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        //底部状态栏
        private void StateBar()
        {
            StatusStrip bar = new StatusStrip();
            this.Controls.Add(bar);
            bar.AutoSize = false;
            bar.Height = 20;
            //当前用户
            ToolStripItem tsi = new ToolStripStatusLabel();
            tsi.AutoSize = false;
            tsi.TextAlign = ContentAlignment.MiddleLeft;
            tsi.Width = 130;
            tsi.Text = "当前用户:" + LoginUser.Name;
            bar.Items.Add(tsi);
            //数据库名称
            tsi = new ToolStripStatusLabel();
            tsi.Text = "数据库名称：" + Config.Sql_database;
            tsi.AutoSize = false;
            tsi.TextAlign = ContentAlignment.MiddleLeft;
            tsi.Width = 180;
            bar.Items.Add(tsi);
            //当前IP
            tsi = new ToolStripStatusLabel();
            tsi.AutoSize = false;
            tsi.TextAlign = ContentAlignment.MiddleLeft;
            tsi.Width = 250;
            tsi.Tag = 0;
            tsi.Click += delegate
            {
                new Thread(new ParameterizedThreadStart(FlushIP)).Start(tsi);
            };
            bar.Items.Add(tsi);
            new Thread(new ParameterizedThreadStart(FlushIP)).Start(tsi);
        }
        private void FlushIP(object obj)
        {
            ToolStripStatusLabel tsi = obj as ToolStripStatusLabel;
            if (tsi == null || Convert.ToInt32(tsi.Tag) == 0)
            {
                Thread.Sleep(200);
                tsi.Tag = 1;
                FlushIP(obj);
            }
            else
            {
                tsi.Text = "刷新中";
                tsi.Text = MyFunction.GetIp();
            }
        }
        //模板制作
        private void TemplateMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(new TemplateMainForm());
        }
        //标签制作
        private void LabelMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(new LabelForm());
        }

        private void TemplateListMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(ListForm.GetInstance("模板"));
        }

        private void LabelListMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(ListForm.GetInstance("标签"));
        }
    }
}
